package com.yc.ui.dialog;

import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.ui.Order;
import com.yc.utils.SWTUtil;
/**
 * 日历弹窗
 * @author lenovo
 *
 */
public class Dialog_Calendar extends Dialog{
    protected Object result;
    protected Shell dialog;
    private DateTime calendar;
    private DateTime date;
    private DateTime time;
    private String selectedDate;// 选择的日期
    private Display display;
    


    /**
     * Create the dialog.
     * @param parent
     * @param style
     */
    public Dialog_Calendar(Shell parent, int style) {
        super(parent, style);
        setText("SWT Dialog");
    }

    /**
     * Open the dialog.
     * @return the result
     */
    public Object open() {
        createContents();
        dialog.open();
        dialog.layout();
        display = getParent().getDisplay();
        while (!dialog.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        return result;    
    }

    /**
     * Create contents of the dialog.
     */
    private void createContents() {
        dialog = new Shell(getParent(),getStyle());
        dialog.setSize(352, 333);

        SWTUtil.centerWindow(dialog);
        
        
        calendar = new DateTime(dialog, SWT.BORDER | SWT.CALENDAR);
        calendar.setBounds(10, 10, 326, 233);
        //监听日期选择
        calendar.addMouseListener(new MouseListener() {
            @Override
            public void mouseUp(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseDown(MouseEvent arg0) {
                int month = (calendar.getMonth() + 1);
                int day = calendar.getDay();
                int hours = time.getHours();
                //这个是直接打印出服务器自动生成的时间
//                selectedDate=calendar.getYear()+"-"+month+"-"+calendar.getDay()+" "
//                        +hours+":"+minutes+":"+time.getSeconds();
                
                //判断如果月，日，时，分，秒是小于10的就在前面加0，这里运用三元运算符进行判断
                selectedDate =  calendar.getYear() + "-" + (month < 10 ? "0" + month : month)+ 
                        "-" + (day<10?"0"+day:day) + " " + (hours<10?"0"+hours:hours);
                
                //如果日历calendar上面切换月份，date的月份也跟着切换
                if(calendar.getMonth()>date.getMonth()){
                    date.setMonth(calendar.getMonth());
                }
                if(calendar.getMonth()<date.getMonth()){
                    date.setMonth(calendar.getMonth());
                }
            }

            @Override
            public void mouseDoubleClick(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }
        });


        date = new DateTime(dialog, SWT.LONG);
        date.setBounds(10, 250, 135, 33);
        //日期选择事件
        date.addSelectionListener(new SelectionListener() {
            @Override
            public void widgetSelected(SelectionEvent arg0) {
                //如果选择date年月,日历calendar的年月也会自动切换
                if(date.getYear() > calendar.getYear()){
                    calendar.setYear(date.getYear());
                }
                if(date.getYear() < calendar.getYear()){
                    calendar.setYear(date.getYear());
                }
                if(date.getMonth() > calendar.getMonth()){
                    calendar.setMonth(date.getMonth());
                }
                if(date.getMonth() < calendar.getMonth()){
                    calendar.setMonth(date.getMonth());
                }
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent arg0) {
                // TODO Auto-generated method stub

            }
        });


        time = new DateTime(dialog, SWT.TIME | SWT.LONG);
        time.setToolTipText("");
        time.setBounds(151, 250, 105, 33);
        

        Button btnOk = new Button(dialog, SWT.NONE);
        btnOk.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
        //确定按钮
        btnOk.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {                
            	//System.out.println(selectedDate);
                //将selectDate字符串传到Test01类下面的方法里面，方便Test调用
            	
            	SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH");
            	Date date = new Date(System.currentTimeMillis()); // 当前日期
            	
            	try {
					Date date1 = formatter.parse(selectedDate);
					if (date1.before(date)) { // 选择的日期比当前日期小
						Order.getDate("选择的日期不能为今天之前");
						return ;
	            	}else {
	            		Order.setDate(selectedDate);
		                dialog.dispose();
	            		
	            	}
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
            }
        });
        btnOk.setBounds(268, 249, 68, 34);
        btnOk.setText("OK");
    }


}

